package com.itxckf.controller;

import com.itxckf.pojo.Comments;
import com.itxckf.pojo.Products;
import com.itxckf.pojo.Result;
import com.itxckf.service.ProductsService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
public class ProductsController {
    @Autowired
    private ProductsService productsService;

    @PostMapping("/getProducts")
    /**
     * 通过 name 搜索
     */
    public Result getProducList(@RequestBody QueryFrom name){
        log.info("获取商品列表,{}",name.getName());
        List<Products> list = productsService.getProductsList(name.getName());
        return Result.success(list);
    };

    @PostMapping("/getProductsListMini")
    /**
     * 通过 name 搜索
     */
    public Result getProductsListMini(@RequestBody QueryFrom name){
        log.info("获取商品列表,{}",name.getName());
        List<Products> list = productsService.getProductsListMini(name.getName());
        return Result.success(list);
    };

    @PostMapping("/addProducts")
    public Result addProducts(@RequestBody Products products){
        log.info("添加商品，{}",products);
        products.setShopID("1");
        productsService.addProducts(products);
        return Result.success("添加成功");
    };


    @PostMapping("/deleteProducts")
    public Result deleteProducts(@RequestBody DeleteFrom delete){
        log.info("删除商品，{}",delete.getId());
        productsService.deleteProducts(delete.getId());
        return Result.success("删除成功");
    };

    @PostMapping("/updateProducts")
    public Result updateProducts(@RequestBody Products products){
        log.info("更新商品，{}",products);
        productsService.updateProducts(products);
        return Result.success("更新成功");
    };

    /**
     * 获取商品的评论
     * @param deleteFrom
     * @return
     */
    @PostMapping("/getProductsComment")
    public Result getProductsComment(@RequestBody DeleteFrom deleteFrom){
        log.info("获取商品的评论，{}",deleteFrom);
        List<Comments> commentsList =  productsService.getProductsComment(deleteFrom.getId());
        return Result.success(commentsList);
    };


    @PostMapping("/addProductsComment")
    public Result addProductsComment(@RequestBody Comments comments){
        log.info("添加商品的评论，{}",comments);
    // 获取当前时间
            Date currentDate = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String formattedDate = formatter.format(currentDate);
        log.info("获取当前时间，{}",formattedDate);
        comments.setCommentTime(formattedDate);
        productsService.addProductsComment(comments);
        return Result.success("添加成功！");
    };

}

@Data
@NoArgsConstructor
@AllArgsConstructor
class QueryFrom {
    private String name;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
class DeleteFrom  {
    private Short id;
}